﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class cgddBLL
    {
       private V_cgddbaobiaoTableAdapter _V_cgddbaobiaoAdapter = null;
       private V_cgddbaobiaoTableAdapter V_cgddbaobiaoAdapter
        {
            get
            {
                if (_V_cgddbaobiaoAdapter == null) _V_cgddbaobiaoAdapter = new V_cgddbaobiaoTableAdapter();
                return _V_cgddbaobiaoAdapter;

            }
        }
       private cgddMasterTableAdapter _cgddMasterlAdapter = null;
       private cgddMasterTableAdapter cgddMasterAdapter
        {
            get
            {
                if (_cgddMasterlAdapter == null) _cgddMasterlAdapter = new cgddMasterTableAdapter();
                return _cgddMasterlAdapter;

            }
        }
       private cgddDetailTableAdapter _cgddDetailAdapter = null;
       private cgddDetailTableAdapter cgddDetailAdapter
        {
            get
            {
                if (_cgddDetailAdapter == null) _cgddDetailAdapter = new cgddDetailTableAdapter();
                return _cgddDetailAdapter;

            }
        }
        /// <summary>
       /// 查询采购订单主表
        /// </summary>
        /// <returns></returns>

       public purchase.cgddMasterDataTable getcgddMasterAll()
        {
            try
            {
                return cgddMasterAdapter.GetDatacgddMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 查询采购订单从表
       /// </summary>
       /// <returns></returns>

       public baobiaoDataSet.V_cgddbaobiaoDataTable getV_cgddbaobiaoAll()
       {
           try
           {
               return V_cgddbaobiaoAdapter.GetDataV_cgddbaobiaoALL ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 查询采购订单从表
       /// </summary>
       /// <returns></returns>

       public baobiaoDataSet.V_cgddbaobiaoDataTable getV_cgddbaobiaoAllBybillNo(string billNo)
       {
           try
           {
               return V_cgddbaobiaoAdapter.GetDataV_cgddbaobiaoBybillNo(billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
       /// 查询采购订单从表
        /// </summary>
        /// <returns></returns>

       public purchase.cgddDetailDataTable getcgddDetailAll()
        {
            try
            {
                return cgddDetailAdapter.GetDatacgddDetailAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据入库单号查询采购订单表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public purchase.cgddMasterDataTable getcgddMasterAllBybillNo(string billNo)
        {
            try
            {
                return cgddMasterAdapter.GetDatacgddMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据入库单号查询采购订单从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public purchase.cgddDetailDataTable getscgddDetailAllBybillNo(string billNo)
        {
            try
            {
                return cgddDetailAdapter.GetDatacgddDetailBybillNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 修改采购订单表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int cgddInsert(cgddMasterModel row, purchase.cgddDetailDataTable pTable, string state, string billNo, string Date)
        {
            cgddDetailModel cgddDetailRow = new cgddDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                cgddMasterDAL cgddMaster = new cgddMasterDAL(tn, conn);
                cgddDetailDAL cgddDetail = new cgddDetailDAL(tn, conn);
                basListDAL basList = new basListDAL(tn, conn);
                if (state == "新增")
                {

                    purchase.cgddMasterDataTable cgddTable = getcgddMasterAllBybillNo(billNo);
                    if (cgddTable.Rows.Count == 0)
                    {
                        cgddMaster.cgddMasterInsert(row);
                    }
                    else
                    {
                        row.billNo = "R" + Date + "-" + basicSql.getOrderSerail("采购订单号");
                        cgddMaster.cgddMasterInsert(row);
                    }

                    //更新单据编号的过程 
                    string orderSerial = basicSql.getOrderSerail("采购订单号");
                    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "采购订单号");
                }
                if (state == "编辑")
                {
                    cgddMaster.cgddMasterUpdate(row);
                }


                foreach (purchase.cgddDetailRow detailRow in pTable)
                {
                    if (detailRow.RowState != DataRowState.Deleted)
                    {
                        cgddDetailRow.recordid = detailRow.recordid;
                        cgddDetailRow.billNo = detailRow.billNo;
                        cgddDetailRow.matNum = detailRow.matNum;
                        cgddDetailRow.matName = detailRow.matName;
                        if (!detailRow.IsmatSpecNull())
                        {
                            cgddDetailRow.matSpec = detailRow.matSpec;
                        }
                        else { cgddDetailRow.matSpec = ""; }
                        cgddDetailRow.qty = detailRow.qty;
                        if (!detailRow.IspriceNull())
                        { cgddDetailRow.price = detailRow.price; }
                        else { cgddDetailRow.price = 0; }

                        cgddDetailRow.currency = "RMB";
                    
                        if (!detailRow.IsacountNull())
                        {
                            cgddDetailRow.acount = detailRow.acount;
                        }
                        else { cgddDetailRow.acount =0; }
                     
                            cgddDetailRow.unit = "KG";
                        
                        if (!detailRow.IsfqtyNull ())
                        {
                            cgddDetailRow.fqty = detailRow.fqty;
                        }
                        else
                        {
                            cgddDetailRow.fqty =0;
                        }
                        if (!detailRow.IsfunitNull ())
                        {
                            cgddDetailRow.funit = detailRow.funit;
                        }
                        else { cgddDetailRow.funit = ""; }
                        cgddDetailRow.createDate = DateTime.Now;
                        if (!detailRow.IsmarkNull())
                        {
                            cgddDetailRow.mark = detailRow.mark;
                        }
                        else
                        {
                            cgddDetailRow.mark = "";
                        }
                        if (!detailRow.IspihaoNull ())
                        {
                            cgddDetailRow.pihao = detailRow.pihao;
                        }
                        else
                        {
                            cgddDetailRow.pihao = "";
                        }
                        if (!detailRow.IsgreadeNull ())
                        {
                            cgddDetailRow.greade = detailRow.greade;
                        }
                        else
                        {
                            cgddDetailRow.greade = "";
                        }
                        if (!detailRow.IspackmarkNull ())
                        {
                            cgddDetailRow.packmark = detailRow.packmark;
                        }
                        else
                        {
                            cgddDetailRow.packmark = "";
                        }
                        if (!detailRow.IsjhDateNull ())
                        {
                            cgddDetailRow.jhDate = detailRow.jhDate;
                        }
                        else
                        {
                            cgddDetailRow.jhDate = "";
                        }
                    }

                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {

                        cgddDetail.cgddDetailInsert(cgddDetailRow);


                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                        cgddDetail.cgddDetailUpdate(cgddDetailRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        DataRow delrow = (DataRow)detailRow;
                        cgddDetail.cgddDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                    }


                }

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行采购订单表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int cgddMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                cgddMasterDAL cgddMaster = new cgddMasterDAL(tn, conn);
                cgddMaster.cgddMasterDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
